package com.nexfra.vocsys.voc.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.nexfra.vocsys.common.Constants;
import com.nexfra.vocsys.common.domain.AttachFile;
import com.nexfra.vocsys.common.domain.User;
import com.nexfra.vocsys.common.service.CommonService;
import com.nexfra.vocsys.common.utils.FileManagement;
import com.nexfra.vocsys.voc.service.VocMyjobMngrService;

@Controller
@RequestMapping(value = "/voc/vocMyjobMngr") 
public class VocMyjobMngrController {
	private static final Logger logger = LoggerFactory.getLogger(VocMyjobMngrController.class);
	
	@Autowired
	VocMyjobMngrService service;

	@Autowired
	CommonService commonService;
	
	/**
	 * VOC등록 - 본인 처리
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/saveSelf")
	@ResponseBody
	@Transactional(rollbackFor = Exception.class)
	public Map<String,Object> saveSelf(@RequestParam HashMap<String, String> params
			,HttpServletRequest request, HttpServletResponse response
			,ModelMap model) throws Exception {
		logger.info("requestParam:"+params.toString());
		logger.info("requestParameterMap:"+request.getParameterMap());
		
		MultipartHttpServletRequest mpRequest = (MultipartHttpServletRequest)request;
		
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute(Constants.USER_KEY);
				
		// file test
	    Iterator<String> files1 =  mpRequest.getFileNames();
	    
		while(files1.hasNext()) {	
			MultipartFile mpf = mpRequest.getFile(files1.next());
			
			logger.info("===================================");
			logger.info(""+mpf.getContentType());
			logger.info(""+mpf.getOriginalFilename());
			logger.info(""+mpf.getSize());
			logger.info(""+mpf.getName());
		}
		
		Map<String, MultipartFile> files = mpRequest.getFileMap();
		List<AttachFile> uploadedAttachFiles = FileManagement.uploadFile(mpRequest, "VOC", files, null); // 저장될 위치,MultipartFile 리스트,저장될 파일이름,제한할 파일사이즈(10485760B=>10MB)
		
		logger.info(uploadedAttachFiles + "");
		
		service.saveSelf(params, user, uploadedAttachFiles);
		
		// voc id에 해당하는 첨부파일 조회
		List<AttachFile> attachFiles = commonService.selectAttachFile(params);		
		
		model.addAttribute("message", Constants.SUCCESS_MESSAGE)
		 .addAttribute("attachFiles", attachFiles);
		return model;
	}	

	/**
	 * VOC등록 - 접수요청
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/saveReq")
	@ResponseBody
	@Transactional(rollbackFor = Exception.class)
	//public String saveReq(@RequestParam HashMap<String, String> params
	public Map<String,Object> saveReq(@RequestParam HashMap<String, String> params
			,HttpServletRequest request, HttpServletResponse response
			,ModelMap model) throws Exception {
		logger.info("requestParam:"+params.toString());
		logger.info("requestParameterMap:"+request.getParameterMap());
		
		MultipartHttpServletRequest mpRequest = (MultipartHttpServletRequest)request;
		
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute(Constants.USER_KEY);
		
		// file test
		Iterator<String> files1 =  mpRequest.getFileNames();
		
		while(files1.hasNext()) {	
			MultipartFile mpf = mpRequest.getFile(files1.next());
			
			logger.info("===================================");
			logger.info(""+mpf.getContentType());
			logger.info(""+mpf.getOriginalFilename());
			logger.info(""+mpf.getSize());
			logger.info(""+mpf.getName());
		}
		
		Map<String, MultipartFile> files = mpRequest.getFileMap();
		List<AttachFile> uploadedAttachFiles = FileManagement.uploadFile(mpRequest, "VOC", files, null); // 저장될 위치,MultipartFile 리스트,저장될 파일이름,제한할 파일사이즈(10485760B=>10MB)
		
		logger.info(uploadedAttachFiles + "");
		
		service.saveReq(params, user, uploadedAttachFiles);
		
/*if(true) {	
	throw new GenericException("익셉션Test");
}*/

		// voc id에 해당하는 첨부파일 조회
		List<AttachFile> attachFiles = commonService.selectAttachFile(params);		
		
		model.addAttribute("message", Constants.SUCCESS_MESSAGE)
		 .addAttribute("attachFiles", attachFiles);
		return model; 
	}	  

}